<?php

class CommunitiesController extends AppController {

    var $name = "Communities";
    public $helpers = array('Html', 'Form');

    public function index($company_id) {
        $this->layout = "ajax";
        $this->set('company_id', $company_id);
        $this->Community->recursive = -1;
        App::Import('model', 'Property');
        $Property = new Property();
        $this->set('communities', $this->Community->find('all', array('conditions' => array('Community.company_id' => $company_id))));
    }

    public function add($company_id) {
        $this->layout = "ajax";
        $this->set('company_id', $company_id);
        if (!empty($this->request->data)) {

            if ($this->Community->save($this->request->data)) {
                $this->Session->setFlash('la informacion se ha agregado correctamente.');
                $this->redirect(array('action' => 'index', $this->data['Community']['company_id']));
            } else {
                $this->Session->setFlash('La informacion  no se  ha agregado.');
                $this->redirect(array('action' => 'index', $this->data['Community']['company_id']));
            }
        }
    }

    function delete($id,$company_id) {
        if ($this->Community->delete($id,$company_id)) {
            $this->Session->setFlash('Registro eliminado.');
            $this->redirect(array('action' => 'index',$company_id));
        }
    }

    public function edit($id) {
        $this->layout = "ajax";
        $this->Community->id = $id;
        if (empty($this->data)) {

            $this->data = $this->Community->find('first', array('conditions' => array('Community.id' => $id)));
        } else {

            if ($this->Community->save($this->data)) {
                $this->Session->setFlash('La informacion ha sido editada.');
                $this->redirect(array('action' => 'index', $this->data['Community']['company_id']));
            }
        }
    }

    public function view($id) {
        $this->layout = "ajax";
        $this->set('community', $this->Community->find('first', array('conditions' => array('Community.id' => $id))));
    }

}

?>
